package com.opencloud.tenant.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.tenant.client.model.SysUserAccount;
import com.opencloud.tenant.client.model.entity.SysUser;
import com.opencloud.tenant.client.service.ISysUserServiceClient;
import com.opencloud.tenant.server.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 租户用户表 前端控制器
 *
 * @author yhg
 * @date 2021-03-11
 */
@Api(value = "租户用户表", tags = "租户用户表")
    @RestController
@RequestMapping("sysUser")
    public class SysUserController implements ISysUserServiceClient {

    @Autowired
    private SysUserService targetService;

    /**
    * 获取分页数据
    *
    * @return
    */
    @ApiOperation(value = "获取分页数据", notes = "获取分页数据")
    @GetMapping(value = "/list")
    public ResultBody<IPage<SysUser>>list(@RequestParam(required = false) Map map){
        PageParams pageParams = new PageParams(map);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper();
        return ResultBody.ok().data(targetService.page(pageParams,queryWrapper));
    }

    /**
     * 根据ID查找数据
     */
    @ApiOperation(value = "根据ID查找数据", notes = "根据ID查找数据")
    @ResponseBody
    @RequestMapping("/get")
    public ResultBody<SysUser> get(@RequestParam("id") Long id){
        SysUser entity = targetService.getById(id);
        return ResultBody.ok().data(entity);
    }

    /**
    * 添加数据
    * @return
    */
    @ApiOperation(value = "添加数据", notes = "添加数据")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "tenantId", required = true, value = "所属租户", paramType = "form"),
         @ApiImplicitParam(name = "userName", required = true, value = "登录账号", paramType = "form"),
         @ApiImplicitParam(name = "realName", required = true, value = "真实姓名", paramType = "form"),
         @ApiImplicitParam(name = "password", required = true, value = "密码", paramType = "form"),
         @ApiImplicitParam(name = "salt", required = true, value = "md5密码盐", paramType = "form"),
         @ApiImplicitParam(name = "avatar", required = true, value = "头像", paramType = "form"),
         @ApiImplicitParam(name = "birthday", required = true, value = "生日", paramType = "form"),
         @ApiImplicitParam(name = "sex", required = true, value = "性别(0-默认未知,1-男,2-女)", paramType = "form"),
         @ApiImplicitParam(name = "email", required = true, value = "电子邮件", paramType = "form"),
         @ApiImplicitParam(name = "phone", required = true, value = "电话", paramType = "form"),
         @ApiImplicitParam(name = "orgCode", required = true, value = "机构编码", paramType = "form"),
         @ApiImplicitParam(name = "status", required = true, value = "状态(1-正常,2-冻结)", paramType = "form"),
         @ApiImplicitParam(name = "delFlag", required = true, value = "删除状态(0-正常,1-已删除)", paramType = "form"),
         @ApiImplicitParam(name = "activitiSync", required = true, value = "同步工作流引擎(1-同步,0-不同步)", paramType = "form"),
         @ApiImplicitParam(name = "createBy", required = true, value = "创建人", paramType = "form"),
        @ApiImplicitParam(name = "updateBy", required = true, value = "更新人", paramType = "form")
            })
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "tenantId") String tenantId,
        @RequestParam(value = "userName") String userName,
        @RequestParam(value = "realName") String realName,
        @RequestParam(value = "password") String password,
        @RequestParam(value = "salt") String salt,
        @RequestParam(value = "avatar") String avatar,
        @RequestParam(value = "birthday") Date birthday,
        @RequestParam(value = "sex") Boolean sex,
        @RequestParam(value = "email") String email,
        @RequestParam(value = "phone") String phone,
        @RequestParam(value = "orgCode") String orgCode,
        @RequestParam(value = "status") Boolean status,
        @RequestParam(value = "delFlag") Boolean delFlag,
        @RequestParam(value = "activitiSync") Boolean activitiSync,
        @RequestParam(value = "createBy") String createBy,
        @RequestParam(value = "updateBy") String updateBy
            ){
        SysUser entity = new SysUser();
        entity.setTenantId(tenantId);
        entity.setUserName(userName);
        entity.setRealName(realName);
        entity.setPassword(password);
        entity.setSalt(salt);
        entity.setAvatar(avatar);
        entity.setBirthday(birthday);
        entity.setSex(sex);
        entity.setEmail(email);
        entity.setPhone(phone);
        entity.setOrgCode(orgCode);
        entity.setStatus(status);
        entity.setDelFlag(delFlag);
        entity.setActivitiSync(activitiSync);
        entity.setCreateBy(createBy);
        entity.setUpdateBy(updateBy);
        targetService.save(entity);
        return ResultBody.ok();
    }

    /**
    * 更新数据
    * @return
    */
    @ApiOperation(value = "更新数据", notes = "更新数据")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "id", required = true, value = "主键id", paramType = "form"),
                    @ApiImplicitParam(name = "tenantId", required = true, value = "所属租户", paramType = "form"),
                    @ApiImplicitParam(name = "userName", required = true, value = "登录账号", paramType = "form"),
                    @ApiImplicitParam(name = "realName", required = true, value = "真实姓名", paramType = "form"),
                    @ApiImplicitParam(name = "password", required = true, value = "密码", paramType = "form"),
                    @ApiImplicitParam(name = "salt", required = true, value = "md5密码盐", paramType = "form"),
                    @ApiImplicitParam(name = "avatar", required = true, value = "头像", paramType = "form"),
                    @ApiImplicitParam(name = "birthday", required = true, value = "生日", paramType = "form"),
                    @ApiImplicitParam(name = "sex", required = true, value = "性别(0-默认未知,1-男,2-女)", paramType = "form"),
                    @ApiImplicitParam(name = "email", required = true, value = "电子邮件", paramType = "form"),
                    @ApiImplicitParam(name = "phone", required = true, value = "电话", paramType = "form"),
                    @ApiImplicitParam(name = "orgCode", required = true, value = "机构编码", paramType = "form"),
                    @ApiImplicitParam(name = "status", required = true, value = "状态(1-正常,2-冻结)", paramType = "form"),
                    @ApiImplicitParam(name = "delFlag", required = true, value = "删除状态(0-正常,1-已删除)", paramType = "form"),
                    @ApiImplicitParam(name = "activitiSync", required = true, value = "同步工作流引擎(1-同步,0-不同步)", paramType = "form"),
                    @ApiImplicitParam(name = "createBy", required = true, value = "创建人", paramType = "form"),
                    @ApiImplicitParam(name = "updateBy", required = true, value = "更新人", paramType = "form")
        })
        @PostMapping("/update")
        public ResultBody add(
                @RequestParam(value = "id") Long id,
                @RequestParam(value = "tenantId") String tenantId,
                @RequestParam(value = "userName") String userName,
                @RequestParam(value = "realName") String realName,
                @RequestParam(value = "password") String password,
                @RequestParam(value = "salt") String salt,
                @RequestParam(value = "avatar") String avatar,
                @RequestParam(value = "birthday") Date birthday,
                @RequestParam(value = "sex") Boolean sex,
                @RequestParam(value = "email") String email,
                @RequestParam(value = "phone") String phone,
                @RequestParam(value = "orgCode") String orgCode,
                @RequestParam(value = "status") Boolean status,
                @RequestParam(value = "delFlag") Boolean delFlag,
                @RequestParam(value = "activitiSync") Boolean activitiSync,
                @RequestParam(value = "createBy") String createBy,
                @RequestParam(value = "updateBy") String updateBy
        ){
            SysUser entity = new SysUser();
                    entity.setId(id);
                    entity.setTenantId(tenantId);
                    entity.setUserName(userName);
                    entity.setRealName(realName);
                    entity.setPassword(password);
                    entity.setSalt(salt);
                    entity.setAvatar(avatar);
                    entity.setBirthday(birthday);
                    entity.setSex(sex);
                    entity.setEmail(email);
                    entity.setPhone(phone);
                    entity.setOrgCode(orgCode);
                    entity.setStatus(status);
                    entity.setDelFlag(delFlag);
                    entity.setActivitiSync(activitiSync);
                    entity.setCreateBy(createBy);
                    entity.setUpdateBy(updateBy);
                targetService.updateById(entity);
                return ResultBody.ok();
        }

    /**
    * 删除数据
    * @return
    */
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "id", paramType = "form")
    })
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id") Long id
    ){
            targetService.removeById(id);
            return ResultBody.ok();
      }

    /**
    * 批量删除数据
    * @return
    */
    @ApiOperation(value = "批量删除数据", notes = "批量删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true, value = "多个用,号隔开", paramType = "form")
    })
    @PostMapping("/batch/remove")
    public ResultBody batchRemove(
                @RequestParam(value = "ids") String ids
            ){
            targetService.removeByIds(Arrays.asList(ids.split(",")));
            return ResultBody.ok();
     }


    /**
     * 获取登录账号信息
     *
     * @param username 登录名
     * @return
     */
    @ApiOperation(value = "获取账号登录信息", notes = "仅限系统内部调用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", required = true, value = "登录名", paramType = "path"),
    })
    @PostMapping("/login")
    @Override
    public ResultBody<SysUserAccount> userLogin(@RequestParam(value = "username") String username) {
        SysUserAccount account = targetService.login(username);
        if (account==null){
            return ResultBody.failed().msg("用户不存在");
        }
        return ResultBody.ok().data(account);
    }

}
